﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Client.Entities;
using PPI.Client.Logic;
using PPI.Controls.Grid;
using PPI.Entities;
using PPIControls.ReportFilters;


namespace PPI.Controls
{
    public partial class ChooseReport : UserControl
    {
        private EReport m_SelectedReport;
        public EReport SelectedReport
        {
            get { return m_SelectedReport; }
            set { m_SelectedReport = value; }
        }
        private DataSet m_DataSource;
        public DataSet DataSource
        {
            get { return m_DataSource; }
        }

        public event EventHandler<ChooseReportEventArgs> SizePanelFilterChanged;
        private void OnSizePanelFilterChanged(object o, ChooseReportEventArgs e)
        {
            // Safely invoke an event:
            if (SizePanelFilterChanged != null)
            {
                SizePanelFilterChanged(o, e);
            }
        }

        public event EventHandler LoadReport;
        private void OnLoadReport(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (LoadReport != null)
            {
                LoadReport(o, e);
            }
        }

        public ChooseReport()
        {
            InitializeComponent();
            this.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        }

        public void LoadCombo()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ReportType", typeof(EReport)));
            dt.Columns.Add(new DataColumn("ReportValue", typeof (string)));
            dt.Columns.Add(new DataColumn("ReportText", typeof(string)));
            dt.Columns.Add(new DataColumn("ReportSource", typeof(string)));

            List<EReport> listReports = LReport.GetAllReports();

            foreach (EReport report in listReports)
            {
                dt.Rows.Add(report, report.FileName, report.Title, report.Datasource);
            }

            ddlReport.DataSource = dt;
            ddlReport.DisplayMember = "ReportText";
            ddlReport.ValueMember = "ReportValue";
        }

        public void GetDatasetFromReport()
        {
            DataRowView drv = (DataRowView)ddlReport.SelectedItem;
            m_SelectedReport = (EReport) drv.Row["ReportType"];
            if (!m_SelectedReport.HasParameters)
                m_DataSource = LReport.getReportDataset(m_SelectedReport.ReportsType, 6);
            else
                m_DataSource =
                    LReport.getReportDatasetWithParameters(m_SelectedReport.ReportsType, ((ReportFilterBaseControl)panelFilterReport.Controls[0]).FilterValues);

        }

        private void btnLoad_Click(object sender, EventArgs e)
        {

            try
            {
                GetDatasetFromReport();
                OnLoadReport(this,new EventArgs());
            }
            catch (Exception ex)
            {
                    
            }



        }

        private void ddlReport_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(ddlReport.SelectedItem != null)
            {
                panelFilterReport.Controls.Clear();
                panelFilterReport.Height = 0;
                DataRowView drv = (DataRowView)ddlReport.SelectedItem;
                EReport tmpReport = (EReport)drv.Row["ReportType"];
                ReportFilterBaseControl tmpControlFilterReport = null;
                tmpControlFilterReport = ReportFilterCreator.GetFilterControl(tmpReport.ReportsType);
                if(tmpControlFilterReport != null && tmpReport.HasParameters)
                {
                    tmpControlFilterReport.Location = new Point(0,0);
                    tmpControlFilterReport.Dock = DockStyle.Fill;
                    panelFilterReport.AutoSize = false;
                    panelFilterReport.Height = tmpControlFilterReport.Height;
                    panelFilterReport.Controls.Add(tmpControlFilterReport);
                }
                OnSizePanelFilterChanged(this,new ChooseReportEventArgs(panelFilterReport.Height + panelSelectorReport.Height + 10));
            }
        }
    }
}
